import { createStore } from 'vuex'

// Create a new store instance.
const store = createStore({
  //设置全局数据的地方
  state: {
    count: 0,
    message:"helloworld",
    postList:[],
    user: JSON.parse(localStorage.getItem('user')),
    online: 0,
    signInfo: {
      is_sign: false,
      signInfo: {
        days: 0,
        reward: 0,
        sign_date: ""
      }
    },
    navnum: 0,
    navtab: 0,
    tokeninfo: JSON.parse(localStorage.getItem('tokeninfo')),
  },
  getters: {
    totalPrice(state){
       return state.count*100
     },
    getUser (state) {
      return state.user
    },
    getOnline (state) {
      return state.online
    },
    getSignInfo (state) {
      return state.signInfo
    },
    getNavnum (state) {
      return state.navnum
    },
    getNavtab (state) {
      return state.navtab
    },
    getTokeninfo (state) {
      return state.tokeninfo
    },
  },
  //修改状态的地方,同步
  mutations: {
    setCount (state) {
      state.count++
    },
    setCountNum(state,num){
      state.count+=num
    },
    //通过actions异步设置
    setMessage(state, value){
      state.message = value;
    },
    //通过actions异步设置
    setPostList(state, list){
      state.postList = list;
    },
    //设置用户登录信息
    setUser(state,user){
      console.log("setUser:",user)
      localStorage.setItem('user',JSON.stringify(user))
      state.user = user
    },
    //设置在线人数
    setOnline (state,num) {
      console.log("setOnline:",num)
      state.online = num
    },
    //设置签到信息
    setSignInfo (state,info) {
      console.log("setSignInfo:",info)
      state.signInfo = info
    },
    //设置主页导航编号
    setNavnum (state,num) {
      console.log("setNavnum:",num)
      state.navnum = num
    },
    //设置状态导航编号
    setNavtab (state,num) {
      console.log("setNavtab:",num)
      state.navtab = num
    },
     //设置token信息
     setTokeninfo(state,tokeninfo){
      console.log("setTokeninfo:",tokeninfo)
      localStorage.setItem('tokeninfo',JSON.stringify(tokeninfo))
      state.tokeninfo = tokeninfo
    },
  },
  //ajax修改状态的地方,异步
  actions: {
    setMessage(context,value){
      context.commit('setMessage',value);
    },
    setPostList(context,list){
      context.commit('setPostList',list);
    }
  },
  modules: {

  }
})

export default store